﻿@{
    ViewBag.Title = "省市区县信息维护";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">行政区划结构</h3>
                <div class="box-tools pull-right">
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                    <a class="btn btn-danger  btn-sm" data-widget="collapse" data-toggle="tooltip" title="伸缩" style="margin-right: 5px;"><i class="fa fa-minus"></i></a>
                </div>
            </div>
            <div class="box-body" id="tree-box-body" style="overflow: auto">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="col-md-9">

        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">行政区划列表</h3>
                <div class="box-tools">
                    <div class="input-group" style="width: 250px;">
                        <input type="text" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                        <div class="input-group-btn">
                            <a class="btn btn-sm btn-default"><i class="fa fa-search"></i></a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-body no-padding">
                <div class="mailbox-controls">
                    <div class="btn-group" id="partial_button_toolbar">
                        @{ Html.RenderPartial("~/Areas/Common/Views/Shared/_PartialButton.cshtml"); }
                    </div>
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                </div>
                <div class="mailbox-jqgrid">
                    <table id="list"></table>
                    <div id="pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        height = UtilWindowHeightWidth().WinH - 218;
        $("#tree-box-body").height(height);
        initGird();
        initTree();
        initTreeData();
    });

    var zNodes,
        treeObj,
        setting,
        $grid,
        height;



    //初始化表格
    function initGird() {
        $grid = $("#list").jgridview(
        {
            multiselect: false,
            url: "/System/District/GetDistrictByParentId",
            colModel: [
                { label: "代码", name: "DistrictId", width: 90 },
                { label: "名称", name: "Name", width: 80, fixed: true },
                { label: "简称", name: "ShortName", width: 80, fixed: true },
                { label: "城市代码", name: "CityCode", width: 60, fixed: true },
                { label: "邮编", name: "ZipCode", width: 60, fixed: true },
                { label: "经度", name: "Lng", width: 100, fixed: true },
                { label: "纬度", name: "Lat", width: 100, fixed: true },
                { label: "拼音", name: "PinYin", width: 100, fixed: true },
                {
                    label: "冻结",
                    name: "IsFreeze",
                    width: 50,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                 { label: "排序号", name: "OrderNo", width: 50, fixed: true, sorttype: "int" }
            ],
            height: height - 100,
            postData: { id: '0' },
            rowList: [200, 500, 1000]
        });
    }

    //初始化组织机构
    function initTree() {
        //配置
        setting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },
            async: {
                enable: true, //开启异步加载
                url: "/System/District/GetDistrictTreeByParentId", //读取数据地址
                autoParam: ["id"], //后台传递的参数
                otherParam: {}, //额外参数
                type: "post", //请求类型
                dataType: "json"
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            expandSpeed: "",
            callback: {
                onClick: onClickTree
            }
        };
    }

    //树点击触发
    function onClickTree() {
        setHiddenPidValue();
        getGridData();
    }

    //初始化树结构
    function initTreeData() {
        UtilAjaxPostAsync("/System/District/GetDistrictTreeByParentId", { id: '0' }, function (data) {
            zNodes = data;
            treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
        });
    }

    //获取表格数据
    function getGridData() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var pId;
        if (treeNode.length === 0) {
            pId = '0';
        } else {
            pId = treeNode[0].id;
        }
        UtilAjaxPost("/System/District/GetDistrictByParentId", { id: pId }, function (data) {
            GridReloadLoadOnceData($grid, data);
        });
    }

    //赋予隐藏值数据
    function setHiddenPidValue() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var pId;
        if (treeNode.length === 0) {
            pId = 0;
        } else {
            pId = treeNode[0].id;
        }
        $("#hiddenPid").val(pId);
        return pId;
    }

    //操作:新增
    function add() {
        ArtDialogOpen("/System/District/Edit?parentId=" + setHiddenPidValue(), "新增省市县", 590);
    }

    //操作:编辑
    function edit() {
        //查看是否选中
        GridIsSelect($grid, function () {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/District/Edit?parentId=" + setHiddenPidValue() + "&districtId=" + info.DistrictId, "编辑省市县-" + info.Name, 590);
        });
    }

    //删除匹配项
    function del() {
        //查看是否选中
        GridIsSelect($grid, function () {
            ArtDialogConfirm("此操作为级联删除,将删除对应的子项!删除后不可恢复,确认要删除?", function () {
                UtilAjaxPostWait(
                    "/System/District/DeleteDistrict",
                    { districtId: GridGetSingSelectData($grid).DistrictId },
                    perateStatus
                );
            });
        });
    }

    //请求完成
    function perateStatus(data) {
        DialogAjaxResult(data);
        if (data.ResultSign === 0) {
            loadTreeAndGrid();
        }
    }

    //加载页面数据
    function loadTreeAndGrid() {
        initTree();
        initTreeData();
        getGridData();
    }

    //重新计算树高度宽度
    function reloadTreeSpace() {
        $("#tree").height($("#uiWest").height() - 38).width($("#uiWest").width() - 10);
    }
</script>